package com.txx.app.main.modules.web.function;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.rsq.function.txxpluginsdk.user.bean.TXXUser;
import com.txx.app.main.App;
import com.txx.app.main.commom.bean.H5GetUserInfoResult;
import com.txx.app.main.commom.bean.H5UserInfoBean;
import com.txx.app.main.modules.web.WebCommonFragment;
import com.txx.app.main.modules.web.js.H5BasePlugin;
import com.txx.app.main.modules.web.js.H5Event;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * created by houyl
 * on  6:51 PM
 */
public class H5GetUserInfoExternal extends H5BasePlugin {

    @Override
    public boolean handleEvent(H5Event h5Event, WebCommonFragment fragment) {
        if (h5Event == null) {
            return false;
        }
        try {

            String json = h5Event.getJsonSource ();
            JSONObject jsonObject = new JSONObject (json);
            String callBackName = jsonObject.getString ("callBackName");
            //        处理后调用
            if (fragment != null) {
                fragment.getH5CallBackCommon (callBackName, new Gson ().toJson (sendH5UserInfoToExternal ()));
            }
        } catch (JSONException e) {
            e.printStackTrace ();
        } catch (Exception e) {
            e.printStackTrace ();
        }

        return true;
    }

    @Override
    public void onRelease() {

    }

    @Override
    public String getCallBackName() {
        return null;
    }


    public H5GetUserInfoResult sendH5UserInfoToExternal(){
        H5GetUserInfoResult h5GetUserInfoResult = new H5GetUserInfoResult();
        if (TextUtils.isEmpty(App.staffBuidler)) {
            h5GetUserInfoResult.setCode("-100");
            h5GetUserInfoResult.setDesc("获取用户信息失败");
            H5UserInfoBean bean = new H5UserInfoBean();
            h5GetUserInfoResult.setMsg(bean);
        } else {
            TXXUser txxUser = App.perInfo;
            h5GetUserInfoResult.setCode("0");
            h5GetUserInfoResult.setDesc("成功获取用户信息");
            H5UserInfoBean bean = new H5UserInfoBean();
            bean.setChannel(txxUser.getChannel());
            bean.setCompanyCode(txxUser.getOrgCode());
            bean.setRoles(txxUser.getRoles());
            bean.setUserCode(txxUser.getStaffNumber());
            bean.setUserName(txxUser.getName());
            h5GetUserInfoResult.setMsg(bean);
        }
        return h5GetUserInfoResult;
    }


}
